package com.iflysse.dao;

import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.UpdateUserPwdPoReq;
import com.iflysse.pojo.po.req.UpdateuserEmailPoReq;
import com.iflysse.pojo.po.req.UserPoReq;
import com.iflysse.pojo.po.resp.UserAccountIdAndRolePoResp;
import com.iflysse.pojo.po.resp.UserPoResp;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.Date;

@Repository
public interface UserDao {

    /**
     * 根据account_number 和 password 或者email 和password登录
     * @param userPoReq
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_account where account_number = #{userAccountInfo} and password = #{passWord} or email = #{userAccountInfo} and password= #{passWord}")
    @Results({
            @Result(property = "accountId",column = "account_id"),
            @Result(property = "accountNumber",column = "account_number"),
            @Result(property = "lastLoginDate",column = "last_login_date"),
    })
    UserPoResp findUserByAccountInfoAndPassword(UserPoReq userPoReq);
    /**
     * 根据accountId 查询用户
     * @param userPoReq
     * @return
     */
    @Select("select * from tb_account where account_id= #{accountId} and role=#{role}")
    @Results({
            @Result(property = "accountId",column = "account_id"),
            @Result(property = "accountNumber",column = "account_number"),
            @Result(property = "lastLoginDate",column = "last_login_date"),
    })
    UserPoResp findUserByAccountIdAndRole(UserPoReq userPoReq)throws SQLException;

    /**
     * 修改密码
     * @param updateUserPwdPoReq
     * @return
     */
    @Update("update tb_account set password = #{password} where account_id = #{accountId}")
    int updatePassword(UpdateUserPwdPoReq updateUserPwdPoReq) throws SQLException;

    /**
     * 修改账户的邮箱
     * @param updateuserEmailPoReq  accountid和email
     * @return
     */
    @Update("update tb_account set email = #{email} where account_id = #{accountId}")
    int updateEmail(UpdateuserEmailPoReq updateuserEmailPoReq) throws SQLException;

    /**
     * 根据账户编号获取最后登录时间
     * @param accountId 账户编号
     * @return
     */
    @Select("select last_login_date from tb_account where account_id = ?")
    Date getLastLoginDate(String accountId) throws SQLException;

    /**
     * 更新登录时间
     * @param accountId 账户编号
     * @return
     */
    @Update("update tb_account set last_login_date = NOW() where account_id = #{accountId}")
    int updateLoginDate(String accountId) throws SQLException;


    /**
     * 根据邮箱用户的账户编号和角色
     * @param email 邮箱
     * @return
     */
    @Select("select account_id,role from tb_account where email = #{email}")
    @Results({
            @Result(property = "accountId",column = "account_id"),
    })
    UserAccountIdAndRolePoResp getUserAccountIdAndRoleByEmail(String email) throws SQLException;

}
